package com.sino.operation.util;

import com.alibaba.fastjson.JSON;
import com.sino.operation.commons.ConstantValue;
import com.sino.operation.config.persistence.MyPersistence;
import com.sino.operation.entity.BizOperateLog;
import com.sino.operation.entity.BizTranslist;
import com.sino.operation.service.impl.BizOperateLogServiceImpl;
import com.sino.operation.service.impl.BizTranslistServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: TaskTerminalUtil
 * @Description: 任务终止服务类
 * @Author dengls
 * @date 2021/10/28 19:40
 */
@Service
public class TaskTerminalUtil {
    @Autowired
    private MyPersistence myPersistence;
    @Autowired
    private BizTranslistServiceImpl transListService;

    /**
     * @param journalNo         流水号
     * @param processInstanceId 工作流唯一id
     * @param terminateTeller   终止柜员
     * @param terminateOrgan    终止机构
     * @param terminatePost     终止岗位
     * @param terminateReason   终止原因
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Map taskTerminal(String journalNo, final String processInstanceId, String terminateTeller,
                            String terminateOrgan, String terminatePost, String terminateReason,
                            String terminateSource) {
        //为流水表赋值
        BizTranslist transList = transListService.getById(journalNo);
        transList.setTerminateTellerNo(terminateTeller);
        transList.setTerminateReason(terminateReason);
        transList.setTransState(ConstantValue.FLOW_STATE_TERMINATED);
        transList.setTerminateTime(TimeUtil.getStandardTime());
        transList.setTerminateSource(terminateSource);

        //为操作日志赋值
        BizOperateLog operateLog = new BizOperateLog();
        operateLog.setId(StringUtil.getId());
        operateLog.setJournalNo(journalNo);
        operateLog.setOperDate(TimeUtil.getStandardTime());
        operateLog.setTellerNo(terminateTeller);
        operateLog.setOrganId(terminateOrgan);
        operateLog.setTellerPost(terminatePost);
        operateLog.setFlowId(processInstanceId);
        operateLog.setOperateType(ConstantValue.OPER_TERMINATE);

        List<Object> data = new ArrayList<>();
        data.add(transList);
        data.add(operateLog);
        myPersistence.save(data);

        RestTemplate restTemplate = new RestTemplate();
        HashMap<String, Object> stopFlowParam = new HashMap<>(16);
        stopFlowParam.put("processInstanceId", processInstanceId);
        String result = restTemplate.postForObject(ConstantValue.FLOWMANAGER + ConstantValue.STOP_TASK, stopFlowParam, String.class);
        return JSON.parseObject(result, Map.class);
    }
}
